unit u_dm_geral;

interface

uses
  SysUtils, Classes, DBXpress, DB, SqlExpr, FMTBcd, Provider, DBClient,
  ZAbstractConnection, ZConnection, Controls;

type
  TdmGeral = class(TDataModule)
    SQLConnectionBD: TSQLConnection;
    sqlConsultaEmp: TSQLQuery;
    cdsConsultaEmp: TClientDataSet;
    dspConsultaEmp: TDataSetProvider;
    cdsConsultaEmpCOD_EMPRESA: TIntegerField;
    cdsConsultaEmpCOD_SINGULAR: TIntegerField;
    cdsConsultaEmpRAZAO_SOCIAL: TStringField;
    cdsConsultaEmpNOME_FANTASIA: TStringField;
    cdsConsultaEmpCNPJ: TStringField;
    cdsConsultaEmpIE: TStringField;
    cdsConsultaEmpTEL_COMERCIAL: TStringField;
    cdsConsultaEmpTEL_FAX: TStringField;
    cdsConsultaEmpTEL_SAC: TStringField;
    cdsConsultaEmpCEP: TStringField;
    cdsConsultaEmpENDERECO: TStringField;
    cdsConsultaEmpNUMERO: TStringField;
    cdsConsultaEmpBAIRRO: TStringField;
    cdsConsultaEmpLOCALIDADE: TStringField;
    cdsConsultaEmpUF: TStringField;
    cdsConsultaEmpOBS: TStringField;
    cdsConsultaEmpDATA_ALTERACAO: TDateField;
    cdsConsultaEmpWEBSITE: TStringField;
    cdsConsultaEmpRESPONSAVEL: TStringField;
    cdsConsultaEmpRESPONSAVEL_EMAIL: TStringField;
    cdsConsultaEmpRESPONSAVEL_TELEFONE: TStringField;
    cdsConsultaEmpRESPONSAVEL_CELULAR: TStringField;
    cdsConsultaEmpAGENTE_AUTORIZADO: TStringField;
    cdsConsultaEmpAGENTE_CONSULTOR: TStringField;
    cdsConsultaEmpAGENTE_CONSULTOR_EMAIL: TStringField;
    cdsConsultaEmpAGENTE_CONSULTOR_TELEFONE: TStringField;
    cdsConsultaEmpSISTEMA_IMPLANTACAO: TDateField;
    cdsConsultaEmpSISTEMA_ULT_BACKUP: TDateField;
    cdsConsultaEmpSISTEMA_ULT_ATUALIZACAO: TDateField;
    cdsConsultaEmpSISTEMA_VERSAO: TStringField;
    cdsConsultaEmpSISTEMA_BLOQUEIO: TDateField;
    cdsConsultaEmpSISTEMA_SMS_LOGIN: TStringField;
    cdsConsultaEmpSISTEMA_SMS_SENHA: TStringField;
    ZConnectionMysql: TZConnection;
    cdsConsultaEmpDESCRICAO_MULTIEMPRESA: TStringField;
    cdsUsuario: TClientDataSet;
    cdsUsuarioCOD_USUARIO: TIntegerField;
    cdsUsuarioDESCRICAO: TStringField;
    cdsUsuarioLOGIN: TStringField;
    cdsUsuarioSENHA: TStringField;
    cdsUsuarioSENHA_LIBERACAO: TStringField;
    cdsUsuarioSENHA_DATA_EXPIRA: TDateField;
    cdsUsuarioSENHA_ATUALIZAR: TStringField;
    cdsUsuarioPERMITIR_ACESSO: TStringField;
    cdsUsuarioPERMITIR_ACESSO_RESTRITO: TStringField;
    cdsUsuarioALERTA_EMAIL: TStringField;
    cdsUsuarioALERTA_EMAIL_ENVIAR: TStringField;
    cdsUsuarioALERTA_SMS: TStringField;
    cdsUsuarioALERTA_SMS_ENVIAR: TStringField;
    cdsUsuarioFOTO: TStringField;
    cdsUsuarioOBS: TStringField;
    cdsUsuarioDATA_CAD: TDateField;
    cdsUsuarioDATA_ALTERACAO: TDateField;
    dspUsuario: TDataSetProvider;
    sqlUsuario: TSQLQuery;
    sqlConsultaEmpUser: TSQLQuery;
    cdsConsultaEmpUser: TClientDataSet;
    dspConsultaEmpUser: TDataSetProvider;
    cdsConsultaEmpUserCOD_EMPRESA: TIntegerField;
    cdsConsultaEmpUserDESCRICAO_MULTIEMPRESA: TStringField;
    spUsuarioAtualizarSenha: TSQLStoredProc;
    cdsConsultaEmpUserRAZAO_SOCIAL: TStringField;
    cdsConsultaEmpUserNOME_FANTASIA: TStringField;
    cdsConsultaEmpUserCNPJ: TStringField;
    procedure DataModuleCreate(Sender: TObject);

  private

  public
    Versao, BancoDados: String;
    function ConexaoFB: Boolean;
    function AcessoRestrito: boolean;
    function VerificaAcessoRestrito(senha: string): Boolean;
    function VerificaAcessoEmpresa(CodUsuario: integer): Boolean;
    function ValidaUsuarioAcesso(login,senha:string): Boolean;
    procedure AtualizarSenhaAcesso(CodUsuario: integer; senha: string);
  end;

var
  dmGeral: TdmGeral;

implementation

uses lib_ini, u_acessorestrito;

{$R *.dfm}

procedure TdmGeral.DataModuleCreate(Sender: TObject);
begin
  Versao := '1.0.21';
  ConexaoFB;
  cdsConsultaEmp.open;
end;

function TdmGeral.ConexaoFB: Boolean;
begin
  dmGeral.SQLConnectionBD.Connected := false;
  BancoDados := LerArquivoIni('rede.ini','conexao','bancodados');
  if BancoDados = '0' then  // se o arquivo nao for encontrado nem tenta conectar
  begin
    Result := false;
    //MensagemValidacao(1);
    exit;
  end;
  dmGeral.SQLConnectionBD.Params.Values['Database'] := BancoDados;
  Try
    dmGeral.SQLConnectionBD.Connected := True;
    Result := true;
  except
    Result := false;
    //MensagemValidacao(2);
  end;
end;

function TdmGeral.AcessoRestrito: boolean;
begin
  with TFRM_AcessoRestrito.Create(nil) do
  try
    if ShowModal = mrOk then
      if VerificaAcessoRestrito(edtSenha.Text) then Result := true else Result := false;
  finally
    free;
  end;
end;

function TdmGeral.VerificaAcessoRestrito(senha: string): Boolean;
var
   qry: TSQLQuery;
 begin
   qry := TSQLQuery.Create(nil);
   try
      qry.SQLConnection := dmGeral.SQLConnectionBD;
      qry.SQL.Clear;
      qry.SQL.Add('select u.permitir_acesso_restrito from usuario u where u.senha_liberacao = :senha');
      qry.Params[0].AsString := senha;
      qry.Open;
      if qry.IsEmpty then Result := false else Result := qry.Fields[0].AsBoolean;
      qry.Close;
   finally
      FreeAndNil(qry);
   end;
end;

function TdmGeral.ValidaUsuarioAcesso(login, senha: string): Boolean;
begin
  cdsUsuario.Close;
  sqlUsuario.Params[0].AsString  := Trim(login);
  sqlUsuario.Params[1].AsString  := Trim(senha);
  cdsUsuario.Open;
  if cdsUsuario.IsEmpty then Result := false else Result := true;
end;

function TdmGeral.VerificaAcessoEmpresa(CodUsuario: integer): Boolean;
begin
  cdsConsultaEmpUser.Close;
  sqlConsultaEmpUser.Params[0].AsInteger := CodUsuario;
  cdsConsultaEmpUser.Open;
  if cdsConsultaEmpUser.IsEmpty then Result := false else Result := true;
end;

procedure TdmGeral.AtualizarSenhaAcesso(CodUsuario: integer; senha: string);
begin
  spUsuarioAtualizarSenha.Params[0].AsInteger := CodUsuario;
  spUsuarioAtualizarSenha.Params[1].AsString  := senha;
  spUsuarioAtualizarSenha.ExecProc;
end;

end.
